<!DOCTYPE html>
<html class="x-admin-sm">
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.1</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="./css/font.css">
    <link rel="stylesheet" href="./css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript" src="./lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="./js/xadmin.js"></script>
    <script type="text/javascript" src="./js/cookie.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
      <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
      <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
  </head>
  
  <body>
    <div class="x-body" id="app">
        <form class="layui-form">
          <div class="layui-form-item">
              <label for="username" class="layui-form-label">
                  <span class="x-red">*</span>员工名
              </label>
              <div class="layui-input-inline">
                  <input type="text" id="username" v-model="emp.name" required="" lay-verify="required"
                  autocomplete="off" class="layui-input">
              </div>

          </div>
          <div class="layui-form-item">
              <label for="phone" class="layui-form-label">
                  <span class="x-red">*</span>头像
              </label>
              <div class="layui-input-inline">
                  <input type="file" ref="myPhoto" id="phone" name="phone" >
              </div>

          </div>
          <div class="layui-form-item">
              <label  class="layui-form-label">
                  <span class="x-red">*</span>工资
              </label>
              <div class="layui-input-inline">
                  <input type="text"  v-model="emp.salary"
                  autocomplete="off" class="layui-input">
              </div>
              <div class="layui-form-mid layui-word-aux">
                  <span class="x-red">*</span>
              </div>
          </div>
            <div class="layui-form-item">
                <label  class="layui-form-label">
                    <span class="x-red">*</span>年龄
                </label>
                <div class="layui-input-inline">
                    <input type="text"  v-model="emp.age" required=""
                           autocomplete="off" class="layui-input">
                </div>
            </div>

          <div class="layui-form-item">
              <button type="button" @click="update" class="layui-btn" lay-filter="add" lay-submit="">
                  修改
              </button>
          </div>
      </form>
    </div>

  </body>

</html>

<script src="js/vue.js"></script>
<script src="js/axios.min.js"></script>
<script>
    var app = new Vue({
        el: "#app",
        data:{
            emp:{}
        },
        methods:{
            update(){
                if(window.confirm("确定要删除这条ID为"+this.emp.id+"的员工信息吗？")){
                    //新建一个表单（Tips：ajax底层也是传递一个表单）
                    var formData = new FormData();
                    formData.append("id",this.emp.id);
                    formData.append("name",this.emp.name);
                    formData.append("salary",this.emp.salary);
                    formData.append("age",this.emp.age);
                    //this.$refs 代表这个页面所用的vue指令
                    formData.append("photo",this.$refs.myPhoto.files[0]);
                    //上传文件给后端时，请求方式必须是post，enctype必须为multipart/form-data
                    var _this = this;
                    axios({
                        method:"post",
                        url:"http://localhost:8080/emp/updateOne",
                        data:formData,
                        headers:{
                            'content-type':'multipart/form-data'
                        }
                    }).then(res=>{
                        if(res.data.state){
                            alert(res.data.msg);
                            window.opener=null;
                            window.open('','_self');
                            window.close();
                        }else{
                            alert(res.data.msg)
                        }
                    })
                }


            }
        },
        created(){
            var start = location.href.lastIndexOf("=");//start = '=item.id'
            var id = location.href.substring(start+1); // id = 'item.id'
            var _this = this;
            axios.get("http://localhost:8080/emp/findOne?id="+id).then(res=>{
                if(res.data.state){
                    _this.emp = res.data.emp;
                }
            })
        }
    })
</script>